package com.tracy.lifeproject.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tracy.lifeproject.R;
import com.tracy.lifeproject.activity.LogInActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 * 最后个人信息的界面
 */
public class PersonalFragment extends Fragment {


    @InjectView(R.id.iv_head)
    ImageView mIvHead;
    @InjectView(R.id.tv_name)
    TextView mTvName;
    @InjectView(R.id.ll_credit_point)
    LinearLayout mLlCreditPoint;
    @InjectView(R.id.ll_invite)
    LinearLayout mLlInvite;
    @InjectView(R.id.iv_message)
    ImageView mIvMessage;
    @InjectView(R.id.iv_setting)
    ImageView mIvSetting;
    @InjectView(R.id.tb_personal)
    Toolbar mTbPersonal;
    @InjectView(R.id.tl_personal)
    TabLayout mTlPersonal;
    @InjectView(R.id.vp_personal)
    ViewPager mVpPersonal;

    private List<View> mList;
    private Activity mActivity;
    private String[] mTabTitle = new String[]{"创建的清单", "收藏的攻略"};

    public PersonalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_personal, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();

    }

    private void initData() {
        mList = new ArrayList<>();
        mActivity = getActivity();
        mTbPersonal.getBackground().setAlpha(0);

        for (int i = 0; i < mTabTitle.length; i++) {
            View itemView = LayoutInflater.from(mActivity).inflate(R.layout.fragment_personal_vpitem, null);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(mActivity, LogInActivity.class));
                }
            });
            mList.add(itemView);
        }
        mVpPersonal.setAdapter(new MyPagerAdapter());
        mTlPersonal.setupWithViewPager(mVpPersonal);
        for (int i = 0; i < mList.size(); i++) {
            mTlPersonal.getTabAt(i).setText(mTabTitle[i]);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.iv_head, R.id.ll_credit_point, R.id.ll_invite, R.id.iv_message, R.id.iv_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                startActivity(new Intent(mActivity, LogInActivity.class));
                break;
            case R.id.ll_credit_point:
                startActivity(new Intent(mActivity, LogInActivity.class));
                break;
            case R.id.ll_invite:
                startActivity(new Intent(mActivity, LogInActivity.class));
                break;
            case R.id.iv_message:
                break;
            case R.id.iv_setting:
                break;
        }
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View child = mList.get(position);
            container.addView(child);
            return child;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
    }
}
